
% Fill the ACE data during 2003 Halloween storm.


% load the data

data = load('/Users/manojnair/data/ace_data/swepam_mag_oct_28-nov_04_2003_V2.dat');

%File format is:
%
%Year
%Day of year + fraction of day
%Proton density (cm^-3)
%Proton temperature (K)
%Proton speed (km/s)
%Na/Np
%Proton Vx GSE (km/s)
%Proton Vy GSE (km/s)
%Proton Vz GSE (km/s)
%Magnetic field B (nT)
%11-Magnetic field Bx GSE (nT)
%12-Magnetic field By GSE (nT)
%13-Magnetic field Bz GSE (nT)

%MISSING DATA POINTS ARE INDICATED BY A FILL VALUE  OF -9999.9

% remove the missing data
L = data == -9999.9;
data(L) = NaN;


% Calculate IEy and IEz

Ey = data(:,13).*data(:,5);
Ez = data(:,12).*data(:,5);

% fday

fday = datenum(2003,0,data(:,2));


% interpolate data at every 1 minute starting 
fday_n = datenum(2003,10,28,0,0,0):5/1440:datenum(2003,11,4,23,59,0);

% Plasma velocity
L = isnan(data(:,5)) | isnan(data(:,13)) | isnan(data(:,12))  | isnan(data(:,11));


V = interp1(fday(~L), data(~L,5), fday_n, 'linear', 'extrap');
IMFBx =  interp1(fday(~L), data(~L,11), fday_n, 'linear', 'extrap');
IMFBy =  interp1(fday(~L), data(~L,12), fday_n, 'linear', 'extrap');
IMFBz =  interp1(fday(~L), data(~L,13), fday_n, 'linear', 'extrap');

% introduce 1 hour time dealy to account for L1 to bowshock nose. This is
% because the data are merged with OMNI database which is at bowshock's
% nose

fday_shifted = fday_n + 1/24;

% now print the values for Nir to upload

fid = fopen('/Users/manojnair/data/ace_data/ACE_data_2003_for_upload.txt','wt');
for i = 1:length(fday_shifted),
    fprintf(fid,'%20s %5.2f %5.2f %5.2f %5.2f\n',datestr(fday_shifted(i),31),V(i), IMFBx(i),...
        IMFBy(i),IMFBz(i));
end;

fclose(fid)
    
